﻿using CommunityToolkit.Mvvm.ComponentModel;
using ShanNiang.Hosting.Common;
using ShanNiang.Service.Attributes;
using ShanNiang.Service.Constants;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Threading;

namespace ShanNiang.Mvvm.ViewModels
{
    public partial class MenuViewModel : ObservableObject
    {
        public MenuViewModel(MenuControlAttribute attr, object viewContent)
        {
            Name = attr.Name;
            MenuCode = attr.MenuCode;
            Sort = attr.Sort;
            //this.LogoImage = new BitmapImage(new Uri($"/Images/{menuCode}.png", UriKind.Relative));
            LogoIcon = attr.IconChar;
            //ViewContent = new Frame() { Content = viewContent };
            ViewContent = viewContent;
        }
        /// <summary>
        /// 名称
        /// </summary>
        [ObservableProperty]
        private string? _name;
        /// <summary>
        /// 编码
        /// </summary>
        [ObservableProperty]
        private string? _menuCode;
        /// <summary>
        /// 对应的视图
        /// </summary>
        [ObservableProperty]
        private object _viewContent;
        ///// <summary>
        ///// 图片Logo
        ///// </summary>
        //[ObservableProperty]
        //private ImageSource? _logoImage;
        /// <summary>
        /// Logo的图标问班
        /// </summary>
        [ObservableProperty]
        private char _logoIcon;
        /// <summary>
        /// 排序
        /// </summary>
        public int Sort { get; set; }

        //static MenuViewModel()
        //{
        //    MenuControlTypes = new Dictionary<string, NameValue<Type>>();
        //    var entryAssembly = Assembly.GetEntryAssembly();
        //    if (entryAssembly != null)
        //    {
        //        var types = entryAssembly.GetTypes();
        //        foreach (var type in types)
        //        {
        //            var menuControlAttr = type.GetCustomAttribute<MenuControlAttribute>();
        //            if (menuControlAttr != null)
        //            {
        //                //var instance = Activator.CreateInstance(type);
        //                //if (instance != null)
        //                //{
        //                    MenuControlTypes.Add(menuControlAttr.MenuCode, new NameValue<Type>(menuControlAttr.Name, menuControlAttr.Sort, type));
        //                //}

        //            }
        //        }
        //    }
        //}
        //private static Dictionary<string, NameValue<Type>> MenuControlTypes { get; }


        //public static MenuViewModel? Create(string menuCode,UserControl userControl)
        //{

        //    try
        //    {
        //        if (MenuControlTypes.TryGetValue(menuCode, out NameValue<Type>? menu))
        //        {
        //            if (menu != null)
        //            {

        //                return new MenuViewModel(menu.Name, menuCode, menu.Sort, userControl);

        //            }
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        MessageBox.Show(ex.Message);
        //    }
        //    return null;
        //}
    }
}
